package com.ruoyi.lm.controller;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lm.domain.LmConf;
import com.ruoyi.lm.service.ILmConfService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统配置Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/lm/conf")
public class LmConfController extends BaseController
{
    private String prefix = "lm/conf";

    @Autowired
    private ILmConfService lmConfService;

    @RequiresPermissions("lm:conf:view")
    @GetMapping()
    public String conf()
    {
        return prefix + "/conf";
    }

    /**
     * 查询系统配置列表
     */
    @RequiresPermissions("lm:conf:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LmConf lmConf)
    {
        startPage();
        List<LmConf> list = lmConfService.selectLmConfList(lmConf);
        return getDataTable(list);
    }

    /**
     * 导出系统配置列表
     */
    @RequiresPermissions("lm:conf:export")
    @Log(title = "系统配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LmConf lmConf)
    {
        List<LmConf> list = lmConfService.selectLmConfList(lmConf);
        ExcelUtil<LmConf> util = new ExcelUtil<LmConf>(LmConf.class);
        return util.exportExcel(list, "conf");
    }

    /**
     * 新增系统配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统配置
     */
    @RequiresPermissions("lm:conf:add")
    @Log(title = "系统配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LmConf lmConf)
    {
        lmConf.setCreateUser(ShiroUtils.getSysUser().getLoginName());
        return toAjax(lmConfService.insertLmConf(lmConf));
    }

    /**
     * 修改系统配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        LmConf lmConf = lmConfService.selectLmConfById(id);
        mmap.put("lmConf", lmConf);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统配置
     */
    @RequiresPermissions("lm:conf:edit")
    @Log(title = "系统配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LmConf lmConf)
    {
        return toAjax(lmConfService.updateLmConf(lmConf));
    }

    /**
     * 删除系统配置
     */
    @RequiresPermissions("lm:conf:remove")
    @Log(title = "系统配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmConfService.deleteLmConfByIds(ids));
    }
}
